package com.huiyuanai.controller;

import com.huiyuanai.pojo.Login;
import com.huiyuanai.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: liwan
 * @Description:
 * @Date: create in 2022/1/11/011
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping("/user/login")
    public String login(HttpSession session, String username, String password, Model model) {
        System.out.println("执行了哦");
        if (!StringUtils.isEmpty(username)) {
            List<Login> loginAll = loginService.getLoginAll();
            for (Login login : loginAll) {
                if (login.getName().equals(username) && login.getPwd().equals(password)) {
                    session.setAttribute("userName", username);
                    return "redirect:/main.html";
                }
            }
        }
        model.addAttribute("msg", "账号或密码错误");
        return "index";
    }

    @RequestMapping("/user/lignout")
    public String lignout(HttpSession session) {
        session.invalidate();
        return "index";
    }

}
